<template>
  <div id="panel">
    <el-row :gutter="20">
      <!-- 左侧树组件（保留） -->
      <el-col :span="5" class="head-container">
        <tree-select v-model="deptName" @node-click="handleNodeClick" />
      </el-col>
      <!-- 右侧内容区域 -->
      <el-col :span="19" class="body-container">
        <el-card class="queryForm">
          <!-- 查询区域 -->
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            style="margin-bottom: 16px"
          >
            <!-- <el-form-item label="审核步骤">
              <el-select
                v-model="queryParams.auditStep"
                placeholder="请选择"
                style="width: 160px"
              >
                <el-option
                  v-for="item in auditStepOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="日期范围">
              <el-date-picker
                v-model="dateRange"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 220px"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleQuery"
                >查询</el-button
              >
              <el-button
                type="text"
                icon="el-icon-download"
                @click="handleExport"
                v-hasPermi="['system:user:export']"
                >导出</el-button
              >
            </el-form-item>
          </el-form>

          <!-- 表格区域 -->
          <el-table
            id="selectTable"
            :data="tableList.rows"
            border
            style="width: 100%"
          >
            <el-table-column type="index" label="序号" width="60" />
            <el-table-column prop="SegmentName" label="站点" />
            <el-table-column prop="ItemName" label="监测项目" />
            <el-table-column prop="oughtToNum" label="应上传数量" />
            <el-table-column prop="totalCount" label="实际传数量" />
            <el-table-column prop="standardCount" label="合格数量" />
            <!-- <el-table-column prop="validDataCount" label="有效数据数量" />
            <el-table-column prop="dataValidRate" label="数据有效率 (%)" />
            <el-table-column
              prop="stationAvgTransmissionRate"
              label="站点平均传输率 (%)"
            />
            <el-table-column
              prop="stationAvgValidRate"
              label="站点平均有效率 (%)"
            /> -->
          </el-table>

          <!-- 分页 -->
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import dayjs from "dayjs";
import tableToExcel from "@/utils/tableToExcel";
import { getWaterQualityControlList } from "@/api/dataResources/monitorData/section.js";

export default {
  name: "HydrologicStation",
  dicts: ["sys_normal_disable", "sys_user_sex"],
  components: {
    TreeSelect: () => import("./TreeSelect.vue"),
  },
  data() {
    return {
      // 审核步骤下拉选项
      // auditStepOptions: [],
      // 部门名称
      deptName: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表格数据
      tableList: {
        total: 0,
        rows: [],
      },
      total: 0,
      // 日期范围
      dateRange: [],
    };
  },
  created() {
    this.getLastThirtyDaysRange();
  },
  methods: {
    // 获取近30天日期范围
    getLastThirtyDaysRange() {
      const endTime = dayjs().format("YYYY-MM-DD");
      const startTime = dayjs().subtract(29, "day").format("YYYY-MM-DD");
      this.dateRange = [startTime, endTime];
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      if (this.$refs.queryForm) {
        this.$refs.queryForm.resetFields();
      }
      this.getLastThirtyDaysRange();
      this.getList();
    },
    // 导出
    handleExport() {
      // 导出逻辑
      tableToExcel.getExcel("#selectTable", "水质质控");
    },
    // 获取表格数据
    getList() {
      const query = JSON.parse(JSON.stringify(this.queryParams));
      if (this.dateRange.length > 0) {
        query.beginTime = this.dateRange[0];
        query.endTime = this.dateRange[1];
      } else {
        query.beginTime = "";
        query.endTime = "";
      }
      console.log(">>>query", query);

      getWaterQualityControlList(query)
        .then((res) => {
          this.tableList.total = res.total;
          if (res.rows.length > 0) {
            this.tableList.rows = res.rows[0].itemList;
            this.tableList.total = res.rows[0].itemList.length;
          }
        })
        .catch((error) => {
          this.$message.error("获取监测数据失败: " + error.message);
        });
    },
    // 树节点点击
    handleNodeClick(data) {
      this.queryParams.segmentid = data;
      this.handleQuery();
    },
  },
};
</script>

<style lang="scss" scoped>
#panel {
  padding: 16px;
  min-height: calc(100vh - 40px);
}

.head-container {
  padding-right: 16px;
}

.body-container {
  padding-left: 0;
}

.queryForm {
  .el-form {
    margin-bottom: 16px;
  }
  .el-form-item {
    margin-right: 16px;
    margin-bottom: 0;
  }
  .el-form-item__label {
    padding-right: 8px;
  }
}

.el-table {
  margin-top: 8px;
}
</style>
